My frustum culling is culling from the wrong point [SOLVED]

Posted by Xbetas on Game Development See other posts from Game Development or by Xbetas
Published on 2012-10-30T14:53:12Z Indexed on 2012/10/31 11:14 UTC
Read the original article Hit count: 295

Filed under:
|

I'm having problems with my frustum being in the wrong origin. It follows the rotation of my camera but not the position.

In my camera class I'm generating a view-matrix:

void Camera::Update()
{
    UpdateViewMatrix();

    glMatrixMode(GL_MODELVIEW);
    //glLoadIdentity();
    glLoadMatrixf(GetViewMatrix().m);
}

Then extracting the planes using the projection matrix and modelview matrix:

void UpdateFrustum()
{
    Matrix4x4 projection, model, clip;

    glGetFloatv(GL_PROJECTION_MATRIX, projection.m);
    glGetFloatv(GL_MODELVIEW_MATRIX, model.m);

    clip = model * projection;

    m_Planes[RIGHT][0] = clip.m[ 3] - clip.m[ 0];
    m_Planes[RIGHT][1] = clip.m[ 7] - clip.m[ 4];
    m_Planes[RIGHT][2] = clip.m[11] - clip.m[ 8];
    m_Planes[RIGHT][3] = clip.m[15] - clip.m[12];
    NormalizePlane(RIGHT);

    m_Planes[LEFT][0] = clip.m[ 3] + clip.m[ 0];
    m_Planes[LEFT][1] = clip.m[ 7] + clip.m[ 4];
    m_Planes[LEFT][2] = clip.m[11] + clip.m[ 8];
    m_Planes[LEFT][3] = clip.m[15] + clip.m[12];
    NormalizePlane(LEFT);

    m_Planes[BOTTOM][0] = clip.m[ 3] + clip.m[ 1];
    m_Planes[BOTTOM][1] = clip.m[ 7] + clip.m[ 5];
    m_Planes[BOTTOM][2] = clip.m[11] + clip.m[ 9];
    m_Planes[BOTTOM][3] = clip.m[15] + clip.m[13];
    NormalizePlane(BOTTOM);

    m_Planes[TOP][0] = clip.m[ 3] - clip.m[ 1];
    m_Planes[TOP][1] = clip.m[ 7] - clip.m[ 5];
    m_Planes[TOP][2] = clip.m[11] - clip.m[ 9];
    m_Planes[TOP][3] = clip.m[15] - clip.m[13];
    NormalizePlane(TOP);

    m_Planes[NEAR][0] = clip.m[ 3] + clip.m[ 2];
    m_Planes[NEAR][1] = clip.m[ 7] + clip.m[ 6];
    m_Planes[NEAR][2] = clip.m[11] + clip.m[10];
    m_Planes[NEAR][3] = clip.m[15] + clip.m[14];
    NormalizePlane(NEAR);

    m_Planes[FAR][0] = clip.m[ 3] - clip.m[ 2];
    m_Planes[FAR][1] = clip.m[ 7] - clip.m[ 6];
    m_Planes[FAR][2] = clip.m[11] - clip.m[10];
    m_Planes[FAR][3] = clip.m[15] - clip.m[14];
    NormalizePlane(FAR);
}

void NormalizePlane(int side)
{
    float length =  1.0/(float)sqrt(m_Planes[side][0] * m_Planes[side][0] +
                                m_Planes[side][1] * m_Planes[side][1] +
                                m_Planes[side][2] * m_Planes[side][2]);

    m_Planes[side][0] *= length;
    m_Planes[side][1] *= length;
    m_Planes[side][2] *= length;
    m_Planes[side][3] *= length;
}

And check against it with:

bool PointInFrustum(float x, float y, float z)
{
    for(int i = 0; i < 6; i++)
    {
        if( m_Planes[i][0] * x + m_Planes[i][1] * y + m_Planes[i][2] * z + m_Planes[i][3] <= 0 )
        return false;
    }
    return true;
}

Then i render using:

camera->Update();
UpdateFrustum();

int numCulled = 0;

for(int i = 0; i < (int)meshes.size(); i++)
{
    if(!PointInFrustum(meshCenter.x, meshCenter.y, meshCenter.z))
    {
        meshes[i]->SetDraw(false); 
        numCulled++;
    }
    else
        meshes[i]->SetDraw(true);
}

Matrices look like (Camera is at (5, 0, 0)):

ModelView [0,0,0.99,0] [0,1,0,0] [-0.99,0,0,0] [0,0,-5,1]

Projection [0.814,0,0,0] [0,1.303,0,0] [0,0,-1,0] [0,0,-0.02,0]

Clip [0,0,-1,-0.999] [0,1.30,0,0] [-0.814,0,0,0] [0,0,4.98,4.99]

What am i doing wrong?

© Game Development or respective owner

Related posts about culling

Related posts about frustum